from marshmallow import fields
from applications.models import User
from applications.extensions import ma


# 用户models的序列化类
class EquipmentOutSchema(ma.Schema):
    id = fields.Integer()
    name = fields.Str()
    username = fields.Method("get_usernames")
    notes = fields.Str()
    enable = fields.Integer()
    create_time = fields.DateTime()
    device_no = fields.Str()

    def get_usernames(self, obj):
        if obj.user_id != None:
            return User.query.filter_by(id=obj.user_id).first().username
        else:
            return None
